//
//  ViewController.swift
//  horizontaltableview
//
//  Created by 火星人 on 2017/6/24.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
//import QSExtensionSwift

class ViewController: UIViewController, QHorizontalTableViewDelegate {

    @IBOutlet weak var htable: QHorizontalTableView!
    var items = [ItemModel]()
    var cells = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        let horizontalTableView = QHorizontalTableView(frame: CGRect(x: 0, y: 100, width: self.view.frame.width, height: 100))
//        horizontalTableView.useCacheForViewLayout = true
//        horizontalTableView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.3)
//        
//        self.view.addSubview(horizontalTableView)
//        
//        horizontalTableView.register(MyCell.classForCoder(), forCellWithReuseIdentifier: "MyCell")
//        horizontalTableView.tableViewDelegate = self
        
        htable.tableViewDelegate = self
        htable.register(MyCell.classForCoder(), forCellWithReuseIdentifier: "MyCell")
        
        initData()
        
    }
    
    
    private func initData(){
        let colors = [UIColor.blue, UIColor.green, UIColor.red, UIColor.cyan, UIColor.brown]
        for i in 1 ... 20 {
            let model = ItemModel()
            model.color = colors[i % colors.count]
            items.append(model)
        }
    }
    
    
    func tableViewItemsCount(_ tableView: QHorizontalTableView) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: QHorizontalTableView, widthForItemAt index: Int) -> CGFloat {
        return self.view.frame.size.width
//        return CGFloat(40 * (index + 1))
    }
    
    
    func tableView(_ tableView: QHorizontalTableView, cellForItemAt index: Int) -> QHorizontalTableViewCell {
        let cell = tableView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: index) as! MyCell
        
        
        cell.backgroundColor = items[index].color
        let str = String(format: "%p", cell)
//        print(str)
        if cells.contains(str) {
            print("use old cell ------- \(cells.count)")
        }
        else{
            cells.append(str)
            print("new cell")
        }
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
        view.backgroundColor = UIColor.yellow
        cell.selectedBackgroundView = view
        cell.label.text = "\(index)"
        
        
        return cell
    }
    
    func tableView(_ tableView: QHorizontalTableView, didSelectRowAt index: Int) {
        print(index)
    }
    
    func tableView(_ tableView: QHorizontalTableView, willDisplay cell: QHorizontalTableViewCell, forItemAt index: Int) {
        
    }
    
    deinit {
        print("deinit view controller")
    }

}

class MyCell: QHorizontalTableViewCell {
    
    var label: UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    private func setup(){
        label = UILabel()
        self.addSubview(label)
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 20)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        label.frame = self.bounds
    }
}

